package com.game.logic.items.title;

import com.xs.action.exection.ActionException;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;
import java.util.Objects;


public interface UserTitleDao extends MongoRepository<UserTitle, Integer> {

    default UserTitle findMust(Integer userInfoId, Integer title){
        return findById(Objects.hash(userInfoId, title)).orElseThrow(() -> new ActionException("未拥有该称号"));
    }

    List<UserTitle> searchAllByUserInfoId(Integer userInfoId);

    boolean existsByUserInfoIdAndTitle(Integer userInfoId, Integer title);

    /// 查看拥有的称号列表
    @Aggregation(pipeline = {
            "{ $match: { 'userInfoId': ?0 } }",
            "{ $project: { 'title': 1 } }"
    })
    List<Integer> searchAllTitleByUserInfoId(Integer userInfoId);
}
